#include <QAction>
#include <QToolButton>

#include <controls/menu/cxitoplevelmenu.h>
#include <private/menu/cxitoplevelmenu/cxitoplevelmenu_p.h>


CxiTopLevelMenu::CxiTopLevelMenu(QWidget* parent)
	: QWidget(parent), d_ptr(new CxiTopLevelMenuPrivate(this))
{
}

CxiTopLevelMenu::~CxiTopLevelMenu() = default;

void CxiTopLevelMenu::addItem(QAction* act)
{
	Q_D(CxiTopLevelMenu);
	act->setCheckable(true);
	const auto toolBtn = new QToolButton;

	d->m_actionGroup->addAction(act);
	toolBtn->setDefaultAction(act);
	toolBtn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);

	connect(act, &QAction::triggered, [this, act]() {
		emit itemClicked(act);
	});

	const int index = d->m_hLayout->count() - 1;
	d->m_hLayout->insertWidget(index, toolBtn);
}

void CxiTopLevelMenu::addItems(const QList<QAction*>& items)
{
	for (const auto& item: items)
	{
		addItem(item);
	}
}
int CxiTopLevelMenu::itemSpacing() const
{
	Q_D(const CxiTopLevelMenu);
	return d->m_hLayout->spacing();
}

void CxiTopLevelMenu::setItemSpacing(int spacing)
{
	Q_D(CxiTopLevelMenu);
	d->m_hLayout->setSpacing(spacing);
}

void CxiTopLevelMenu::resizeEvent(QResizeEvent* event)
{
	Q_D(CxiTopLevelMenu);
	QWidget::resizeEvent(event);
	d->updateLayout();
}